chore: refactor permission rules and add additional validation#7844
chore: refactor permission rules and add additional validation#7844jeffsmale90 merged 18 commits intomainfrom
Conversation
4cd5553 to
df72ecd
Compare
a9450f5 to
8700f23
Compare
packages/gator-permissions-controller/src/decodePermission/rules/erc20TokenPeriodic.test.ts
Show resolved
Hide resolved
52df231 to
e1cb91e
Compare
packages/gator-permissions-controller/src/decodePermission/rules/erc20TokenStream.ts
Show resolved
Hide resolved
| }); | ||
|
|
||
| it('rejects when startTime is 0', () => { | ||
| const tokenAddress = '0xdddddddddddddddddddddddddddddddddddddddd' as Hex; |
There was a problem hiding this comment.
I think the valid 20-byte addresses should be 40 hex chars.
| const tokenAddress = '0xdddddddddddddddddddddddddddddddddddddddd' as Hex; | |
| const tokenAddress = '0xdddddddddddddddddddddddddddddddddddddd' as Hex; |
There was a problem hiding this comment.
I think it is 40 hex chars:
> "0xdddddddddddddddddddddddddddddddddddddddd".length
42
"0x" followed by 40 "d"s is 42 chars
| }); | ||
|
|
||
| it('rejects when startTime is 0', () => { | ||
| const tokenAddress = '0xdddddddddddddddddddddddddddddddddddddddd' as Hex; |
There was a problem hiding this comment.
| const tokenAddress = '0xdddddddddddddddddddddddddddddddddddddddd' as Hex; | |
| const tokenAddress = '0xdddddddddddddddddddddddddddddddddddddd' as Hex; |
There was a problem hiding this comment.
Lol, I counted those ds very carefully!
There was a problem hiding this comment.
Actually, the existing string if 42 chars, which is 20 bytes, which is correct.
I added validation to the erc20 permission decoders to ensure that the token addresses are the correct length.
There was a problem hiding this comment.
Actually, I didn't, because we can't validate individual component lengths (the bytes are concatenated), but we do have terms length validation for each permission type.
packages/gator-permissions-controller/src/decodePermission/rules/nativeTokenPeriodic.ts
Show resolved
Hide resolved
| splitHex(terms, [20, 32, 32, 32]); | ||
| const periodDuration = hexToNumber(periodDurationRaw); | ||
| const startTime = hexToNumber(startTimeRaw); | ||
|
|
There was a problem hiding this comment.
should we add periodAmount > 0 check here?
packages/gator-permissions-controller/src/decodePermission/rules/nativeTokenPeriodic.ts
Outdated
Show resolved
Hide resolved
packages/gator-permissions-controller/src/decodePermission/rules/erc20TokenPeriodic.test.ts
Show resolved
Hide resolved
packages/gator-permissions-controller/src/decodePermission/rules/erc20TokenStream.ts
Show resolved
Hide resolved
9990e4f to
5ff2c48
Compare
packages/gator-permissions-controller/src/decodePermission/rules/erc20TokenStream.test.ts
Show resolved
Hide resolved
packages/gator-permissions-controller/src/decodePermission/rules/nativeTokenStream.ts
Show resolved
Hide resolved
packages/gator-permissions-controller/src/decodePermission/rules/nativeTokenStream.ts
Outdated
Show resolved
Hide resolved
packages/gator-permissions-controller/src/decodePermission/rules/erc20TokenStream.ts
Show resolved
Hide resolved
…ion to ensure that permission data invariants are not violated.
…ype is self-describing and can be more easily tested in isolation. Add validation and test coverage for each permission type.
Plus minor changes: - Remove redundant amendment to ChecksumCaveat type - Remove unused ValidateDecodedPermission type - Fixes controller tests that expected the controller to self-report GatorPermissionsSnap id - Make decode functions internal, and rename to align with public interface
…e ChecksumEnforcersByChainId type rather than explicitly declaring a new type
…ithout specifying snapId
- use metamask/utils isHexAddress instead of regex to validate addresses - use hexToBigInt to validate tokenPeriod instead of hexToNumber
830e176 to
feff8e1
Compare
feff8e1 to
c7ba989
Compare
packages/gator-permissions-controller/src/decodePermission/rules/erc20TokenPeriodic.test.ts
Show resolved
Hide resolved
MoMannn
left a comment
There was a problem hiding this comment.
isSubset is now dead code and can be removed
| ); | ||
| } | ||
|
|
||
| if (periodAmountBigInt <= 0n) { |
There was a problem hiding this comment.
These can check only if === 0n. There are no negative numbers in solidity so its not possible for them to be less then 0.
There was a problem hiding this comment.
true - they're being decoded from hex so there's no way to encode a negative
|
|
||
| const EXPECTED_VALUE_LTE_TERMS_BYTELENGTH = 32; | ||
|
|
||
| if (getByteLength(valueLteTerms) !== EXPECTED_VALUE_LTE_TERMS_BYTELENGTH) { |
There was a problem hiding this comment.
This check is redundant. The next if is validating valueLteTerms !== ZERO_32_BYTES which also validates the length.
| * const result = matchingRules[0].validateAndDecodePermission(caveats); | ||
| * if (result.isValid) { ... result.expiry, result.data ... } | ||
| * | ||
| * getPermissionRuleMatchingCaveatTypes and getPermissionDataAndExpiry use these rules |
There was a problem hiding this comment.
I believe these functions got removed now?
There was a problem hiding this comment.
I removed this comment entirely as it's not really relevant with the refactor.
- checks now check value === 0 instead of <= 0 because hex encoding is always >= 0 - remove redundant terms length check
be36041 to
41c7393
Compare
packages/gator-permissions-controller/src/decodePermission/rules/nativeTokenStream.ts
Show resolved
Hide resolved
…tion comparing maxAmount and initialAmount.
2d3f8f7 to
c6577ca
Compare
packages/gator-permissions-controller/src/decodePermission/rules/erc20TokenPeriodic.test.ts
Show resolved
Hide resolved
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
Explanation
It's critical that the
GatorPermissionController's Permission decoding logic is strict, and will not decode EIP-712 payload to a permission unless the payload exactly meets the expectations of that permission.This PR refactors the permission rules to be more self contained, and self describing. This allows each permission type's decoding rules to be more thoroughly tested in isolation. Validation and decoding logic is combined, as decoding is an implicit part of the validation step.
This PR also adds explicit validation of the "implicit" caveats for each permission type (
valueLtefor ERC20 permissions,exactCalldatafor native token permissions), where previously we were ensuring that they caveats exist, but not validating their terms.References
Checklist
Note
High Risk
Changes the core permission-type identification and caveat decoding/validation path; this could cause previously accepted permission contexts to be rejected or misclassified if rule matching/term checks are incorrect.
Overview
Refactors permission decoding to be rule-driven and stricter.
GatorPermissionsController.decodePermissionFromPermissionContextForOriginnow builds per-chain permission rules and selects a single matching rule viafindRuleWithMatchingCaveatAddresses, then validates+decodes caveat terms throughrule.validateAndDecodePermission(replacingidentifyPermissionByEnforcers+getPermissionDataAndExpiry).Adds explicit caveat-term validation per permission type. New rule modules validate enforcer term structure and constraints (e.g.,
ExactCalldataEnforcermust be0x,ValueLteEnforcermust be zero, byte-length checks, positiveperiodAmount/periodDuration/startTime, ERC20tokenAddresshex validation, and streammaxAmount > initialAmount). The test suite is reorganized to test rules in isolation and adds adversarial/negative cases; changelog updated accordingly.Written by Cursor Bugbot for commit 79be687. This will update automatically on new commits. Configure here.